package com.mypage.view.datedialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.cloudcc.mobile.R;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class DateTimePopupWindow extends PopupWindow implements OnClickListener{

	public enum Type
	{
		ALL, YEAR_MONTH_DAY_HOURS, YEAR_MONTH_DAY, HOURS_MINS, MONTH_DAY_HOUR_MIN
	}

	private View rootView;
	DateWheelTime wheelTime;
	private View btnSubmit, btnCancel;
	private static final String TAG_SUBMIT = "submit";
	private static final String TAG_CANCEL = "cancel";
	private OnTimeSelectListener timeSelectListener;
	private String mN;

	@SuppressLint("InflateParams")
	@SuppressWarnings("deprecation")
	public DateTimePopupWindow(String s,Context context, Type type)
	{
		super(context);
		this.mN=s;
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setBackgroundDrawable(new BitmapDrawable());// �������ò��ܵ����Ļ��dismiss����
		this.setOutsideTouchable(true);
		this.setAnimationStyle(R.style.timepopwindow_anim_style);

		LayoutInflater mLayoutInflater = LayoutInflater.from(context);
		if ("M".equals(mN)) {
			rootView = mLayoutInflater.inflate(R.layout.date_ttimepicker_pw_time_tomonth, null);
		}
		else if ("T".equals(mN)) {
			rootView = mLayoutInflater.inflate(R.layout.date_ttimepicker_pw_time_, null);
		}else{
			rootView = mLayoutInflater.inflate(R.layout.date_ttimepicker_pw_time, null);
		}
		
		// -----ȷ����ȡ��ť
		btnSubmit = rootView.findViewById(R.id.btnSubmit);
		btnSubmit.setTag(TAG_SUBMIT);
		btnCancel = rootView.findViewById(R.id.btnCancel);
		btnCancel.setTag(TAG_CANCEL);
		btnSubmit.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		// ----ʱ��ת��
		final View timepickerview = rootView.findViewById(R.id.timepicker);
		DateScreenInfo screenInfo = new DateScreenInfo((Activity) context);
		wheelTime = new DateWheelTime(timepickerview, type);

		wheelTime.screenheight = screenInfo.getHeight();
		// Ĭ��ѡ�е�ǰʱ��
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hours = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		wheelTime.setPicker(year, month, day, hours, minute);

		setContentView(rootView);
	}

	/**
	 * ���ÿ���ѡ���ʱ�䷶Χ
	 * 
	 * @param START_YEAR
	 * @param END_YEAR
	 */
	public void setRange(int START_YEAR, int END_YEAR)
	{
		DateWheelTime.setSTART_YEAR(START_YEAR);
		DateWheelTime.setEND_YEAR(END_YEAR);
	}

	/**
	 * ָ��ѡ�е�ʱ�䣬��ʾѡ����
	 * 
	 * @param parent
	 * @param gravity
	 * @param x
	 * @param y
	 * @param date
	 */
	public void showAtLocation(View parent, int gravity, int x, int y, Date date)
	{
		Calendar calendar = Calendar.getInstance();
		if (date == null)
			calendar.setTimeInMillis(System.currentTimeMillis());
		else
			calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hours = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		wheelTime.setPicker(year, month, day, hours, minute);
		update();
		super.showAtLocation(parent, gravity, x, y);
	}

	/**
	 * ����ѡ��ʱ��
	 * 
	 * @param date
	 */
	public void setTime(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		if (date == null)
			calendar.setTimeInMillis(System.currentTimeMillis());
		else
			calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hours = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		wheelTime.setPicker(year, month, day, hours, minute);
	}

	/**
	 * ָ��ѡ�е�ʱ�䣬��ʾѡ����
	 * 
	 * @param parent
	 * @param gravity
	 * @param x
	 * @param y
	 * @param date
	 */
	public void showAtLocation(View parent, int gravity, int x, int y,
			Date date, boolean isBefore)
	{
		Calendar calendar = Calendar.getInstance();
		if (date == null)
			calendar.setTimeInMillis(System.currentTimeMillis());
		else
			calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day;
		if (isBefore)
		{
			day = calendar.get(Calendar.DAY_OF_MONTH) - 1;
		} else
		{
			day = calendar.get(Calendar.DAY_OF_MONTH);
		}
		int hours = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		wheelTime.setPicker(year, month, day, hours, minute);
		update();
		super.showAtLocation(parent, gravity, x, y);
	}

	/**
	 * �����Ƿ�ѭ������
	 * 
	 * @param cyclic
	 */
	public void setCyclic(boolean cyclic)
	{
		wheelTime.setCyclic(cyclic);
	}

	@Override
	public void onClick(View v)
	{
		String tag = (String) v.getTag();
		if (tag.equals(TAG_CANCEL))
		{
			dismiss();
			return;
		} else
		{
			if (timeSelectListener != null)
			{
				try
				{
					Date date = DateWheelTime.dateFormat.parse(wheelTime.getTime());
					timeSelectListener.onTimeSelect(date);
				} catch (ParseException e)
				{
					e.printStackTrace();
				}
			}
			dismiss();
			return;
		}
	}

	public interface OnTimeSelectListener
	{
		public void onTimeSelect(Date date);
	}

	public void setOnTimeSelectListener(OnTimeSelectListener timeSelectListener)
	{
		this.timeSelectListener = timeSelectListener;
	}


}
